package cn.kgc.ffzw.fuxi.link;

public class MyLink {

    private Entry link;
    private int size;

    public Entry getLink() {
        return link;
    }

    public void setLink(Entry link) {
        this.link = link;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    //向链表的尾部添加一个环
    public void add(Object key,Object value){
        //造一个环
        Entry cpnow=new Entry(key,value);
        //判断列表是否为空
        if(link==null){
            link=cpnow;
        }else{
            Entry last=link;
            while(last.getNext()!=null){
                last=last.getNext();
            }
            //把最后一环的地址给空环的上一环
            cpnow.setPrev(last);
            //再把空环的地址赋给最后一环的下一个地址
            last.setNext(cpnow);

            //把新环插入链表头部
            //将空环的下一个环指向原第一环
            cpnow.setNext(link);
            //将原第一环上的上一个环指向新环
            link.setPrev(cpnow);
            //把link指向最后一环
            link=cpnow;
        }
        size++;
    }

    //删除一环
    public void remove(int pos) throws NullPointerException{
        if(pos>=size){
            throw new NullPointerException("兄弟不要越界");
        }
        if(pos==0){
            //删除首环
            //把首环的下一环的prev设置为null并同时把link指向第二环
            link=link.getNext();
        }else if(pos==size-1){
            //删除尾环
            Entry last=indexOf(size-1);
//            while(last.getNext()!=null){
//                last=last.getNext();
//            }
            last.getPrev().setNext(null);
        }else{
            Entry delEntry=indexOf(pos);
            for (int i = 0; i < pos; i++) {
                delEntry=delEntry.getNext();
            }
            //将删除环的上一环的next指向删除环的next
            delEntry.getPrev().setNext(delEntry.getNext());
        }
        size--;
    }
    //修改

    //根据用户给的第pos环获得对应的Entryd对象
    public Entry indexOf(int pos) throws NullPointerException{
        if(pos>=size){
            throw new NullPointerException("越界");
        }
        Entry last=link;
        for (int i = 0; i < pos; i++) {
            last=last.getNext();
        }
        return last;
    }

    //替换某环
    public void set(int pos,Object key,Object value){
        //找到用户要替换的环
        Entry rep=indexOf(pos);
        //征程新环
        Entry newEntry=new Entry(key,value);
        //将替换环的上一环和下一环赋给新环的上一环和下一环、
        newEntry.setPrev(rep.getPrev());
        newEntry.setNext(rep.getNext());

        //将替换环的上一环的下一环设置为新环
        rep.getPrev().setNext(newEntry);
        //将替换环的下一环的上一环设置为新环
        rep.getNext().setPrev(newEntry);

        //将替换环的上下环设置为null;
        rep.setNext(null);rep.setPrev(null);
    }
    public class Entry{
        private Entry prev;
        private Entry next;
        private Object key;
        private Object value;

        public Entry getPrev() {
            return prev;
        }

        public void setPrev(Entry prev) {
            this.prev = prev;
        }

        public Entry getNext() {
            return next;
        }

        public void setNext(Entry next) {
            this.next = next;
        }

        public Object getKey() {
            return key;
        }

        public void setKey(Object key) {
            this.key = key;
        }

        public Object getValye() {
            return value;
        }

        public void setValye(Object valye) {
            this.value = valye;
        }

        public Entry(Object key, Object valye) {
            this.key = key;
            this.value = valye;
        }
    }


    public static void main(String[] args) {
        MyLink ml = new MyLink();
        ml.add(1,100);
        ml.add('d',100);
        ml.add("三大势力",'f');
        ml.add(1,100);
        System.out.println(ml);
    }
}
